<?php
class Admin_TheChinhController extends Zend_Controller_Action
{
	protected $_arrParam;
    public function init()
    {
       $this->_helper->layout->setLayout('admin');
       $this->_arrParam = $this->_request->getParams();
       $this->view->arrParam = $this->_arrParam;
    }

    public function indexAction()
    {
    	$thechinhMapper = Library_Model_TheChinhMapper::getInstance();
    	$thechinh = $thechinhMapper->thechinhList();
    	$this->view->thechinh = $thechinh;
    	//Zend_Registry::get('logger')->info($thechinh);
    }

    public function xemkhachhangAction(){
    	if($this->getRequest()->isPost())
    	{
    		$id= $this->_arrParam['mathechinh'];
    	
    		/* $chitiettheMapper = Library_Model_ChiTietTheMapper::getInstance();
    		$ttkh = $chitiettheMapper->searchByMathe($id);
    	 */
    	}
    	$this->view->title = 'Xem thông tin khách hàng';
    	/* $this->view->ttkh = $ttkh;
    	
    	Zend_Registry::get('logger')->info($ttkh); */
    }
    
    public function nhanAction(){
    	$this->view->title = "Tiếp nhận thẻ";
    	$dangkytheMapper = Library_Model_DangKyTheMapper::getInstance();    	
    	$danhsachdk = $dangkytheMapper->dangkyList();
    	
    	//Zend_Registry::get('logger')->info($danhsachdk);
    	if($this->getRequest()->isPost())
    	{
    	//	$chitiettheObj = new Library_Model_ChiTietThe(); 

    		$thechinhObj 	= new Library_Model_TheChinh();
    		$khachhangObj = new Library_Model_KhachHang();
    			
    		$khachhangMapper = Library_Model_KhachHangMapper::getInstance();
    		$thechinhMapper = Library_Model_TheChinhMapper::getInstance();
    		
    		foreach($danhsachdk as $danhsach)
    		{
    			$id = $danhsach->getId();
    			
    	
    			$tiepnhan = $this->_arrParam['nhan'.$id];
    			if($tiepnhan == '1')
    			{
    				$hovaten = $danhsach->getHovaten();
    				$ngaysinh = $danhsach->getNgaysinh();
    				$diachithuongtru = $danhsach->getDiachithuongtru();
    				$diachilienlac= $danhsach->getDiachilienlac();
    				$dienthoai= $danhsach->getDienthoai();
    				$cmnd= $danhsach->getCmnd();
    				$ngaycap= $danhsach->getNgaycap();
    				$noicap= $danhsach->getNoicap();
    				$tencoquan = $danhsach->getTencoquan();
    				$diachicoquan= $danhsach->getDiachicoquan();
    				$dienthoaicoquan= $danhsach->getDienthoaicoquan();
    				$machinhanh = $danhsach->getMachinhanh();
    				$ngaydangky = $danhsach->getNgaydangky();
    				
					$khachhangObj->setHovaten($hovaten);
					$khachhangObj->setNgaysinh($ngaysinh);
					$khachhangObj->setCmnd($cmnd);
					$khachhangObj->setNgaycap($ngaycap);
					$khachhangObj->setNoicap($noicap);
					$khachhangObj->setDiachithuongtru($diachithuongtru);
					$khachhangObj->setDiachilienlac($diachilienlac);
					$khachhangObj->setDienthoai($dienthoai);
					$khachhangObj->setTencoquan($tencoquan);
					$khachhangObj->setDienthoaicoquan($dienthoaicoquan);
					$khachhangObj->setDiachicoquan($diachicoquan);
					 
					$khachhang = $khachhangMapper->save($khachhangObj);
				 
					$thechinhObj->setMakhachhang($khachhang);
					$thechinhObj->setMachinhanh($machinhanh);
	    			$thechinhObj->setNgaydangky($ngaydangky);
	    			$thechinh = $thechinhMapper->save($thechinhObj);    	
    						
    				$dangkytheMapper->delete($id);    					
    			}
    		}
    		$this->_redirect('/admin/thechinh/index');
    	}
    	$this->view->danhsach = $danhsachdk;
    }
    
    public function themAction()
    {
    	$this->view->title = 'Đăng ký thẻ ';    	
    	$formThem = new Admin_Form_TheChinhForm();     
    	$formThem->setMethod('post');
    	$formThem->setAction('/zfMHB/public/admin/thechinh/them');
    	
    	$formThem->addElement('submit', 'submit', array('label' => 'Thêm'));
    	
    	$this->view->form = $formThem;
    	
    	if($this->getRequest()->isPost()){
    		if($formThem->isValid($_POST)){
    			$data = $formThem->getValues();
    			
    			$machinhanh			= $data['machinhanh'];

				$hovaten 					= $data['hovaten'];
				$ngaysinh				= $data['ngaysinh'];
				$diachithuongtru = $data['diachithuongtru'];
				$diachilienlac			= $data['diachilienlac'];
				$cmnd 					= $data['cmnd'];
				$ngaycap					= $data['ngaycap'];
				$noicap					= $data['noicap'];				
				$dienthoai				= $data['dienthoai'];
				$tencoquan				= $data['tencoquan'];
				$dienthoaicoquan	=$data['dienthoaicoquan'];
				$diachicoquan			=$data['diachicoquan'];
				
				$thechinhObj 	= new Library_Model_TheChinh();
				$khachhangObj = new Library_Model_KhachHang();
				
				$khachhangMapper = Library_Model_KhachHangMapper::getInstance();
				$thechinhMapper = Library_Model_TheChinhMapper::getInstance();
				
				$khachhangObj->setHovaten($hovaten);
				$khachhangObj->setNgaysinh($ngaysinh);
				$khachhangObj->setCmnd($cmnd);
				$khachhangObj->setNgaycap($ngaycap);
				$khachhangObj->setNoicap($noicap);
				$khachhangObj->setDiachithuongtru($diachithuongtru);
				$khachhangObj->setDiachilienlac($diachilienlac);
				$khachhangObj->setDienthoai($dienthoai);
				$khachhangObj->setTencoquan($tencoquan);
				$khachhangObj->setDienthoaicoquan($dienthoaicoquan);
				$khachhangObj->setDiachicoquan($diachicoquan);
				 
				$khachhang = $khachhangMapper->save($khachhangObj);
				 
				$thechinhObj->setMakhachhang($khachhang);
				$thechinhObj->setMachinhanh($machinhanh);
    			$thechinhObj->setNgaydangky(Zend_Date::now()->get('yyyy-MM-dd HH:mm:ss'));
    			$thechinh = $thechinhMapper->save($thechinhObj);
    			
    			$this->_redirect('/admin/thechinh/index');
    			}    		
    			
    		}
        	//$this->view->dkkhachhang = $dkkhachhang;
    }
/* 
    public function capnhatAction()
    {
        $this->view->title = 'Cập nhật thẻ chính';
        $mathe = $this->getRequest()->getParam('id');
        $formCapnhat = new Admin_Form_TheChinhForm($mathe);
        $formCapnhat->setMethod('post');
        $formCapnhat->setAction('/zfMHB/public/admin/thechinh/capnhat/');
        
        $formCapnhat->addElement('submit', 'submit', array('label' => 'Lưu'));
        $formCapnhat->addElement('hidden', 'id');
       
        
      //$formCapnhat->getElement('hovaten')->getValidator('Db_NoRecordExists')->setMessage('Văn bản đã tồn tại!');
        $thechinhMapper = Library_Model_TheChinhMapper::getInstance();
        $this->view->form = $formCapnhat;
        
        if($this->getRequest()->isPost()){
        	if($formCapnhat->isValid($_POST)){
        		$data = $formCapnhat->getValues();
        		
        		$id 					= $data['id'];
        		$hovaten 			= $data['hovaten'];
    			$ngaysinh		= $data['ngaysinh'];
    			$cmnd 				= $data['cmnd'];
    			$ngaycap			= $data['ngaycap'];
    			$noicap			= $data['noicap'];
				$machinhanh			= $data['machinhanh'];
				$diachithuongtru		= $data['diachithuongtru'];
				$diachilienlac			= $data['diachilienlac'];
				$dienthoai				= $data['dienthoai'];
				$tencoquan				= $data['tencoquan'];
				$dienthoaicoquan	=$data['dienthoaicoquan'];
				$diachicoquan			=$data['diachicoquan'];
				
    			$thechinhObj 	= new Library_Model_TheChinh();

    			$thechinhObj->setId($id);
    			$thechinhObj->setHovaten($hovaten);
				$thechinhObj->setNgaysinh($ngaysinh);
				$thechinhObj->setCmnd($cmnd);
				$thechinhObj->setNgaycap($ngaycap);
				$thechinhObj->setNoicap($noicap);
				$thechinhObj->setMachinhanh($machinhanh);
				$thechinhObj->setDiachithuongtru($diachithuongtru);
				$thechinhObj->setDiachilienlac($diachilienlac);
				$thechinhObj->setDienthoai($dienthoai);
				$thechinhObj->setTencoquan($tencoquan);
				$thechinhObj->setDienthoaicoquan($dienthoaicoquan);
				$thechinhObj->setDiachicoquan($diachicoquan);
				
    	
    			$thechinh = $thechinhMapper->save($thechinhObj);
    			$this->_redirect('/admin/thechinh/index');
        	}
        } else {
        		$thechinh = $thechinhMapper->searchTheChinhById($mathe);
        		
        		 $formCapnhat->getElement('id')->setValue($thechinh->getId());
        		$formCapnhat->getElement('hovaten')->setValue($thechinh->getHovaten());        		
        		$formCapnhat->getElement('ngaysinh')->setValue($thechinh->getNgaysinh());
        		$formCapnhat->getElement('cmnd')->setValue($thechinh->getCmnd());
        		$formCapnhat->getElement('ngaycap')->setValue($thechinh->getNgaycap());
				$formCapnhat->getElement('noicap')->setValue($thechinh->getNoicap());
				$formCapnhat->getElement('machinhanh')->setValue($thechinh->getMachinhanh());
				$formCapnhat->getElement('diachithuongtru')->setValue($thechinh->getDiachithuongtru());
				$formCapnhat->getElement('diachilienlac')->setValue($thechinh->getDiachilienlac());
				$formCapnhat->getElement('dienthoai')->setValue($thechinh->getDienthoai()); 
				$formCapnhat->getElement('tencoquan')->setValue($thechinh->getTencoquan());
				$formCapnhat->getElement('dienthoaicoquan')->setValue($thechinh->getDienthoaicoquan());
				$formCapnhat->getElement('diachicoquan')->setValue($thechinh->getDiachicoquan());
				
			}
        
    } */

    public function xoaAction()
    {
    	$this->_helper->viewRenderer->setNoRender(true);
    	$mathechinh = $this->getRequest()->getParam('id');
    	$thechinhMapper = Library_Model_TheChinhMapper::getInstance();
    	
    	$thechinh = $thechinhMapper->searchTheChinhById($mathechinh);
    	/* $chitietthes = $thechinh->getChitietthe();
    	foreach($chitietthes as $chitietthe){
    		$iddangky = $chitietthe->getId();
    		$chitiettheMapper->delete($iddangky);
    	} */
    	$thechinhMapper->delete($mathechinh);
    	$this->_redirect('/admin/thechinh/index');
    }
    
    
   public function validateformAction()
    {
        $user_id = $this->getRequest()->getParam('id');
        $this->_helper->viewRenderer()->setNoRender();
        $this->_helper->getHelper('layout')->disableLayout();
        $form = new Admin_Form_TheChinhForm();
        $form->isValid($this->_getAllParams());
        $json = $form->getMessages();
        header('Content-type: application/json');
        echo Zend_Json::encode($json);
    }


}









